package lion.framework.core.web.views;

import java.io.FileNotFoundException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lion.dev.web.WebUtil;
import lion.framework.core.conf.Config;
import lion.framework.core.conf.ConfigManager;
import lion.framework.core.web.anno.ViewType;
import lion.framework.core.web.exception.WebException;
import lion.framework.core.web.path.ParameterizedPath;
import lion.framework.core.web.request.RequestModel;

/**
 * 该视图处理JSP页面
 * 
 * @author lion
 * @email hl_0074@sina.com
 * @date 2015年3月18日
 */

@ViewType("jsp")
public class JSPView implements IView {

	public static final String VIEW_TYPE = "jsp";
	private String name;
	private String path;

	public JSPView() {

	}

	public JSPView(String path) {

		this.path = path;
	}

	@Override
	public void render(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String path = ParameterizedPath.mapPath(this.path, new RequestModel(request));
		WebUtil.exposeIncludeAttribute(request);

		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		Config config = ConfigManager.getConfig("framework");
		response.setContentType("text/html;charset=" + config.getString("framework.sysencoding", "UTF-8"));
		try {
			request.getRequestDispatcher(path).include(request, response);
		} catch (Exception e) {
			if (e instanceof FileNotFoundException) { throw new WebException(404, "File Not Found " + path); }
			throw e;
		}
	}

	@Override
	public String type() {

		return VIEW_TYPE;
	}

	@Override
	public String getName() {

		return this.name;
	}

}
